package com.imay.live.manager;

import android.app.Activity;
import android.content.Context;
import android.os.Handler;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.SpannableStringBuilder;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.zhouwei.library.CustomPopWindow;
import com.imay.live.adapter.NoticeBubbleAdapter;
import com.imay.live.model.UnreadModel;
import com.imay.live.R;
import com.imay.base.util.SpannableStringUtils;
import com.imay.live.utils.ViewUtil;

import java.util.ArrayList;
import java.util.List;

import static com.imay.live.manager.UnreadManager.TYPE_UNREAD_MESSAGE;

/**
 * Created by nick on 2017/5/27.
 */

public class NoticeBubbleManager {

    private List<UnreadModel> unreadModels;
    private CustomPopWindow mListPopWindow;
    private NoticeBubbleAdapter bubbleAdapter;
    private RecyclerView rcv_notice_bubble;
    private View contentView;
    private int TIME = 1000 * 5;
    private Handler handler;
    private Runnable runnable;

    /**
     * =========直播通知===========
     */
    private CustomPopWindow mListLivePopWindow;
    private TextView mBubbleNum;
    private View contentLiveView;

    private int x = -1;
    private int y = -1;

    private static class NoticeBubbleManagerHolder {
        private static final NoticeBubbleManager INSTANCE = new NoticeBubbleManager();
    }

    public static final NoticeBubbleManager getInstance() {
        return NoticeBubbleManager.NoticeBubbleManagerHolder.INSTANCE;
    }

    public void showBubbleDialog(Activity activity, UnreadModel model, View view) {
        if (handler == null) {
            initHandle();
        }
        if (rcv_notice_bubble == null) {
            contentView = LayoutInflater.from(activity).inflate(R.layout.dialog_bubble, null);
            rcv_notice_bubble = (RecyclerView) contentView.findViewById(R.id.rcv_notice_bubble);
            LinearLayoutManager manager = new LinearLayoutManager(activity);
            manager.setOrientation(LinearLayoutManager.VERTICAL);
            rcv_notice_bubble.setLayoutManager(manager);
        }
        addData(model);
        bindAdapter(unreadModels);
        showPopListView(activity, view);


    }

    /**
     * 直播提示气泡
     *
     * @param activity
     * @param view
     */
    public void showLiveBubbleDialog(Activity activity, View view, int size) {
        if (activity == null || activity.isFinishing()) {
            return;
        }
        if (mBubbleNum == null) {
            contentLiveView = LayoutInflater.from(activity).inflate(R.layout.dialog_live_bubble, null);
            mBubbleNum = ViewUtil.findView(contentLiveView, R.id.tv_live_bubble_num);
        }
        mBubbleNum.setText(Integer.toString(size));
        //创建并显示popWindow
        if (mListLivePopWindow == null) {
            mListLivePopWindow = new CustomPopWindow.PopupWindowBuilder(activity)
                    .setView(contentLiveView)
                    .setOutsideTouchable(false)
                    .size(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)//显示大小
                    .create();
        }
        mListLivePopWindow.showAsDropDown(view, 0, 5);
        contentLiveView.postDelayed(new Runnable() {
            @Override
            public void run() {
                if (mListLivePopWindow != null) {
                    mListLivePopWindow.dissmiss();
                }
            }
        }, TIME);
    }

    /**
     * 超短播的提示泡
     *
     * @param context
     * @param view
     * @param msg
     */
    public void showChallengeBubbleDialog(Context context, View view, String msg) {
        View contentView = LayoutInflater.from(context).inflate(R.layout.dialog_live_challenge_bubble, null);
        TextView mChallenge = ViewUtil.findView(contentView, R.id.tv_live_challenge);
        mChallenge.setText(msg);
        final CustomPopWindow customPopWindow = new CustomPopWindow.PopupWindowBuilder(context)
                .setView(contentView)
                .setOutsideTouchable(false)
                .setTouchable(false)
                .size(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)//显示大小
                .create();

        if (x == -1 && y == -1) {
            int[] location = new int[2];
            view.getLocationOnScreen(location);
            x = location[0];
            y = location[1];
        }

        contentView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
        int popupWidth = contentView.getMeasuredWidth();    //  获取测量后的宽度
        int popupHeight = contentView.getMeasuredHeight();  //获取测量后的高度

        view.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
        int viewWidth = view.getMeasuredWidth();    //  获取测量后的宽度
        customPopWindow.showAtLocation(view, Gravity.NO_GRAVITY, (x + viewWidth) - popupWidth - 8, y - popupHeight - 4);
        contentView.postDelayed(new Runnable() {
            @Override
            public void run() {
                if (customPopWindow != null) {
                    customPopWindow.dissmiss();
                }
            }
        }, TIME);
    }

    private void addData(UnreadModel model) {
        boolean hasExit = false;
        if (unreadModels == null) {
            unreadModels = new ArrayList<>();
        }
        for (UnreadModel unreadModel : unreadModels) {
            if (unreadModel.getWarnType() == model.getWarnType()) {
                unreadModel.setValue(model.getValue());
                hasExit = true;
            }

        }
        if (!hasExit) {
            unreadModels.add(model);
        }


    }

    private void showPopListView(Activity activity, View view) {
        //创建并显示popWindow
        if (mListPopWindow == null) {
            mListPopWindow = new CustomPopWindow.PopupWindowBuilder(activity)
                    .setView(contentView)
                    .size(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)//显示大小
                    .create();
        }
        if (activity.hasWindowFocus())
            mListPopWindow.showAsDropDown(view, 0, 10);
        handler.postDelayed(runnable, TIME);

    }

    private void bindAdapter(List<UnreadModel> unreadModels) {
        if (bubbleAdapter == null) {
            bubbleAdapter = new NoticeBubbleAdapter(unreadModels);
            rcv_notice_bubble.setAdapter(bubbleAdapter);
        } else {
            bubbleAdapter.setNewData(unreadModels);
        }
    }

    public void setData(List<UnreadModel> unreadModels) {
        if (rcv_notice_bubble != null) {
            bindAdapter(unreadModels);
        }
    }

    private void initHandle() {
        handler = new Handler();
        runnable = new Runnable() {

            @Override
            public void run() {
                try {
                    if (mListPopWindow != null) {
                        mListPopWindow.dissmiss();
                    }

                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    System.out.println("exception...");
                }
            }
        };

    }

    public void clearMineNoticeUnreadData() {
        if (unreadModels == null) {
            return;
        }
        int position = getPositionRecentMessage();
        if (position != -1) {
            UnreadModel model = unreadModels.get(position);
            unreadModels.clear();
            unreadModels.add(model);
            bindAdapter(unreadModels);
        } else {
            unreadModels.clear();
            bindAdapter(unreadModels);
        }

    }

    public void clearRecentMessageNoticeUnreadData() {
        int position = getPositionRecentMessage();
        if (position != -1) {
            unreadModels.remove(position);
            bindAdapter(unreadModels);

        }

    }

    private int getPositionRecentMessage() {
        if (unreadModels != null) {
            for (int i = 0; i < unreadModels.size(); i++) {
                if (unreadModels.get(i).getWarnType() == TYPE_UNREAD_MESSAGE) {
                    return i;
                }
            }
        }
        return -1;
    }

    /**
     * 催直播气泡
     *
     * @param activity
     * @param view
     */
    public void showCallLiveBubbleDialog(Activity activity, View view, int size) {
        if (activity == null || activity.isFinishing()) {
            return;
        }

        View contentCallLiveView = LayoutInflater.from(activity).inflate(R.layout.dialog_call_live_bubble, null);
        TextView tv_call_live_bubble = ViewUtil.findView(contentCallLiveView, R.id.tv_call_live_bubble);
        SpannableStringBuilder builder = SpannableStringUtils.getBuilder(size+"").setForegroundColor(activity.getResources().getColor(R.color.text_ff0)).
                append(activity.getString(R.string.call_live_bubble)).setForegroundColor(activity.getResources().getColor(R.color.text_70)).create();

        tv_call_live_bubble.setText(builder);
        //创建并显示popWindow

        final CustomPopWindow callLivePopWindow = new CustomPopWindow.PopupWindowBuilder(activity)
                    .setView(contentCallLiveView)
                    .setOutsideTouchable(false)
                    .size(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)//显示大小
                    .create();
        callLivePopWindow.showAsDropDown(view, 0, 5);
        contentCallLiveView.postDelayed(new Runnable() {
            @Override
            public void run() {
                if (callLivePopWindow != null) {
                    callLivePopWindow.dissmiss();
                }
            }
        }, TIME);
    }



}
